
package com.example.controller;

import com.example.common.ApiResult;
import com.example.service.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.math.BigInteger;

import io.swagger.v3.oas.annotations.tags.Tag;
/**
 * Token 相关接口
 */
@RestController
@RequestMapping("/api/token")
@Tag(name = "Token管理", description = "ERC20 Token相关接口")
public class TokenController {

    @Resource
    private TokenService tokenService;

    @Operation(summary = "ERC20 Token 转账")
    @org.springframework.web.bind.annotation.PostMapping("/transfer")
    public ApiResult<String> transferToken(@RequestParam String to, @RequestParam BigInteger amount) {
        try {
            String txHash = tokenService.transferToken(to, amount);
            return ApiResult.success(txHash);
        } catch (Exception e) {
            return ApiResult.error(500, "Token转账失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "查询指定地址的ERC20余额")
    @GetMapping("/balance")
    public ApiResult<BigInteger> getTokenBalance(@RequestParam String address) {
        try {
            BigInteger balance = tokenService.getBalance(address);
            return ApiResult.success(balance);
        } catch (Exception e) {
            return ApiResult.error(500, "查询Token余额失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查询代币symbol")
    @GetMapping("/symbol")
    public ApiResult<String> getSymbol() {
        try {
            String symbol = tokenService.getSymbol();
            return ApiResult.success(symbol);
        } catch (Exception e) {
            return ApiResult.error(500, "查询symbol失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查询代币名称")
    @GetMapping("/name")
    public ApiResult<String> getName() {
        try {
            String name = tokenService.getName();
            return ApiResult.success(name);
        } catch (Exception e) {
            return ApiResult.error(500, "查询name失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查询代币decimals")
    @GetMapping("/decimals")
    public ApiResult<BigInteger> getDecimals() {
        try {
            BigInteger decimals = tokenService.getDecimals();
            return ApiResult.success(decimals);
        } catch (Exception e) {
            return ApiResult.error(500, "查询decimals失败: " + e.getMessage());
        }
    }
}
